#!/bin/bash -e

log=.log.`date +%N`
echo `basename $0` $@ >>$log || exit 0;
prog=`basename $0`
args=$@

function info() {
cat /mnt/ilustre/app/medical/tools/script/info.snp.txt
rm $log
exit 65
}

function error() {
echo `basename $0`': invalid option -?'
info
}


while getopts  ":a:s:i:l:d:gkLGnc:Ap:m:q:r:t:" opts
do
        case  $opts  in
		a) cutx=$OPTARG;;
        i)
			interval=$OPTARG
			i=i
		;;
		l)
			target=$OPTARG
			f=f
		;;
		g) single_cell=T;;
		G) somatic=T;;
		d) desti=$OPTARG;;
		k) mark=T;;
		n)
			cutadaptor=$OPTARG
			n=n
		;;
		c)
			cnv=$OPTARG
			c=c
		;;
		r) realn_interval=$OPTARG;;
		m)
			mem=$OPTARG
			m=m
		;;
		p)
			cpu=$OPTARG
			p=p
		;;
		q)
			query=$OPTARG
			q=q
		;;
		A)
			a=n
		;;
		s) sample_name=$OPTARG;;
		L) run_local=T;;
		t) continue_dir=$OPTARG;;
		h) info;;
		\?) error;;
        esac
done
shift $(($OPTIND - 1))

if [ -z "$1" ]; then info; fi

if [ -z "$sample_name" ]; then
if test -d $1; then
sample_name=`fq_name.pl $1`;
if test -z "$sample_name"; then sample_name=test; fi
else sample_name=test
fi
fi

if [ -z "$desti" ]; then desti=snp; fi
if [ -z "$cutx" ]; then cutx=malbac; fi



if test -z "$run_local"; then 
subq.sh -n$sample_name $prog -L $args
# qsid=`cat $qsinfo | awk -F "." '{print $1}'`

clear
echo A snp calling and annotion job, name $sample_name, submitted to pbs...
exit 0
else
echo A snp calling and annotion job run in local...
fi

if
echo $1 |perl -n -e 'if(/^\//){exit 0;}else{exit 2;}'
then
a=$1
else
a=`pwd`/$1
fi

if test -n "$2"; then


if
echo $2 |perl -n -e 'if(/^\//){exit 0;}else{exit 2;}'
then
b=$2
else
b=`pwd`/$2
fi


# b=`pwd`/$2
r12=`echo $a $b`
# echo r12 $r12
elif test -d $1; then
num=`ls -1 $1|wc -l`
	if test $num -eq 2; then
	ab=`ls $a`
	aa=`echo $ab |cut -d' ' -f1`
	bb=`echo $ab |cut -d' ' -f2`
	r12=`echo $a/$aa $a/$bb`
	# echo r12 $r12
	fi
fi

if test -z "$continue_dir"; then
makedir=true
else
echo no make dir
cd $continue_dir
fi


. /mnt/ilustre/app/medical/tools/.var


if [ -n "$p" ]; then
	qsub_cmd="$qsub_cmd -l nodes=1:ppn=$cpu"
fi

if [ -n "$m" ]; then
	qsub_cmd="$qsub_cmd -l mem=${mem}gb"
fi

if [ -n "$q" ]; then
	qsub_cmd="$qsub_cmd -$q $query"
fi



# if [ -n "$i" ]; then
	# ana_cmd="$ana_cmd -$i $interval"
# fi

# if [ -n "$f" ]; then
	# ana_cmd="$ana_cmd -$f $interval"
# fi

# if [ -n "$a" ]; then
	# ana_cmd="$ana_cmd -$a"
# fi

# if [ -n "$n" ]; then
	# ana_cmd="$ana_cmd -$n"
# fi

# if [ -n "$c" ]; then
	# ana_cmd="$ana_cmd -$c $cnv"
# fi



if test "$cutx" = "malbac"; then
	cutadp=cutadp_malbac_170214.sh
elif test "$cutx" = "general"; then
	cutadp=cutadp_170214.sh
fi


if [ "$interval" = "brca12" ]; then
	cutadp=cutadp_170214.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-102X-Covered.b37.bed
elif [ "$interval" = "brca44" ]; then
	cutadp=cutadp_170214.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-001X-Covered.b37.bed
elif [ "$interval" = "ctc80" ]; then
	cutadp=cutadp_ctc80_discard_trimmed.sh
	target=$data_path/intervals/1/ctc80.bed
elif [ "$interval" = "brca12mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/brca12_mor.bed
elif [ "$interval" = "ca50mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/ca50_mor.bed
elif [ "$interval" = "exomes" ]; then
	target=$data_path/intervals/1/b37.agilent_Exon_V5_UTRs.bed
elif [ "$interval" = "wg" ]; then
	target=$data_path/intervals/1/b37.bed
else
	cutadp=cutadp_170214.sh
fi



echo
echo $target
echo $cutadp

# http='http://192.168.10.113:8000'

snapshot=$out_prefix.snapshot.txt
test -e $snapshot || echo -e "Sample: $sample_name\nRaw data: $r12\nPanel: $interval\nResults: ${http}/$desti" >$snapshot

if test $cpu_num -gt 1; then
ts0=`echo ${cpu_num}-3|bc`
else
ts0=1
fi

fastqstat=fastqstat.sh
if
cat $pid_log|grep "^$fastqstat done" >/dev/null; then
echo $fastqstat done
else
fastqstat.sh -s$sample_name -t2 -p$out_prefix $r12 &
fi


# great or equal than 1; 
# because fastqc would run more than one time,
# if it runs 2 times, then it works.
fastqc=fastqc.sh
if test `cat $pid_log|grep "^$fastqc done" |wc -l` -ge 1; then
echo $fastqc done
else
fastqc.sh -s$sample_name $r12 &
fi

if
cat $pid_log|grep "^$cutadp done" >/dev/null; then
echo $cutadp done
else
$cutadp -p$out_prefix $r12
fi
# align0223.sh -p$out_prefix -s$sample_name $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq
aln=aln_bwa_bt.sh
if
cat $pid_log|grep "^$aln done" >/dev/null; then
echo aln_bwa_bt.sh done
else
aln_bwa_bt.sh -t$ts0 -s$sample_name $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq && rm $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq
fi

sam2b=sam2b

if
cat $pid_log|grep "^$sam2b.* done" >/dev/null; then
echo $sam2b done
else

if test `du $out_prefix.sam|cut -f1` -lt 1000000; then

sam2b_picard.sh $out_prefix.sam
else

sam2b_samtools.sh -t$ts0 $out_prefix.sam

fi

fi

# markdup
if test "$mark" = "T"; then
deldup=deldup.sh
	if
	cat $pid_log|grep "^$deldup done" >/dev/null; then
	echo $deldup done
	else
		# delete symbolic link if exists.
		if test -e $out_prefix.markdup.bam; then
			rm $out_prefix.markdup.bam*;
		fi
		
		$deldup -m $out_prefix.sort.bam
	fi

else
	ln -fs $out_prefix.sort.bam $out_prefix.markdup.bam
	if test -e $out_prefix.sort.bam.bai; then
		ln -fs $out_prefix.sort.bam.bai $out_prefix.markdup.bam.bai
	elif test -e $out_prefix.sort.bai; then
		ln -fs $out_prefix.sort.bai $out_prefix.markdup.bai
	fi
fi
# end markdup 

# cnv
if test -n "$cnv"; then
	
	if test -

	if test "$cnv" = "hmmcp"; then

	deldup=deldup.sh
	if
	cat $pid_log|grep "^$deldup done" >/dev/null; then
	echo $deldup done
	else
	deldup.sh $out_prefix.sort.bam
	fi

	hmmcopy0917b38.sh -w50000 -b37 $out_prefix.deldup.bam
	hmmcopy0917b38.sh -w200000 -b37 $out_prefix.deldup.bam
	hmmcopy0917b38.sh -w1000000 -b37 $out_prefix.deldup.bam
	fi
fi
#end cnv

# fastqc.sh -s$sample_name $out_prefix.sort.bam &

realign_bt=realign_bt.sh
if
cat $pid_log|grep "^$realign_bt done" >/dev/null; then
echo $realign_bt done
else

# realign_bt.sh $out_prefix.markdup.bam
if test -n "$realn_interval"; then

if test -n "$target"; then
echo realign with known interval $realn_interval in target $target
realign_bt.sh -r$realn_interval -i$target $out_prefix.markdup.bam
else
realign_bt.sh -r$realn_interval $out_prefix.markdup.bam
fi


elif test -n "$target"; then
echo realign in $target
realign_bt.sh -i$target $out_prefix.markdup.bam
else
echo realign in whole region
realign_bt.sh $out_prefix.markdup.bam
fi

fi

recal=recal0630.sh
if
cat $pid_log|grep "^$recal done" >/dev/null; then
echo $recal done
else

$recal -l$target $out_prefix.realn.bam
fi

if test `cat $pid_log|grep "^$fastqc done" |wc -l` -ge 2; then
echo $fastqc done
else
fastqc.sh -s$sample_name $out_prefix.realn.recal.bam &
fi


cal0=ug.sh
if
cat $pid_log|grep "^$cal0 done" >/dev/null; then
echo $cal0 done
else
	if test -n "$somatic"; then
		echo somatic or low coverage
		$cal0 -G -p$out_prefix -i$target $out_prefix.realn.recal.bam
	else
		$cal0 -p$out_prefix -i$target $out_prefix.realn.recal.bam
	fi
fi

summ=summ.sh
if
cat $pid_log|grep "^$summ done" >/dev/null; then
echo $summ done
else
$tools_path/script/summ.sh -g -s$sample_name $out_prefix.realn.recal.bam $target #can not background

ln $out_prefix.gene_summ.txt gene_summ.txt
fi

# annotation

anno=annos.sh
if
cat $pid_log|grep "^$anno done" >/dev/null; then
echo $anno done
else


if test -n "$somatic"; then
	echo somatic
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$annos -m -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$annos -m -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
else
	echo germline
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$annos -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$annos -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
fi

fi


#format

format_simple=format_simple_0629.sh
if
cat $pid_log|grep "^$format_simple done" >/dev/null; then
echo $format_simple done
else
$format_simple -p$out_prefix.high $out_prefix.high.af.hg19_multianno.vcf &
$format_simple -p$out_prefix.low $out_prefix.low.af.hg19_multianno.vcf &
fi

# format=format_top_0222.sh
# if
# cat $pid_log|grep "^$format done" >/dev/null; then
# echo $format done
# else

# $format $out_prefix.high.vcf $out_prefix.low.vcf
# fi


wait


rename=file_rename_0222.sh
if
cat $pid_log|grep "^$rename done" >/dev/null; then
echo $rename done
else
$rename $sample_name
fi



echo;echo; echo desti: $desti.



bk.sh -t$desti ${sample_name}*simple_info*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*.html .log* ${sample_name}*deleterious.txt ${sample_name}*drugs-prediction*


bk_zip.sh ${sample_name}*simple_info*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*2*.html ${sample_name}*drugs-prediction.html


mail.sh $sample_name $interval $desti/`basename $(pwd)` $r12


. $cmd_done



